<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

$creator_id = UV::readInt(strip_tags($route->GET("user")));
$show = UV::readMQ(strip_tags($route->GET("show")));

$query = "select `N`.*,`U`.`login` from `news` as `N`
left join `users` as `U` on
`N`.`user_id` = `U`.`user_id` where `U`.`type` != 'administrator'";
$query .= ($creator_id ? " and `N`.`user_id` = '$creator_id'" : "");
switch($show)
{
	case "all": $query .= ""; break;
	case "moder": $query .= " and `N`.`moder` = '1'"; break;
	default : $query .= " and `N`.`moder` = '0'";
}
$query .=  " order by `news_id` desc";
$news = $db->getTable($query);

$page = UV::readInt(strip_tags($route->GET("page")));
$limit = 10;
$count = count($news);
$max_page = ceil($count / $limit);
if ($page < 1)
	$page = 1;
elseif ($page > $max_page)
	$page = $max_page;
$query .= " limit ".($page-1)*$limit.",$limit";

$news = $db->getTable($query);

require_once ADMIN.DS."head.php";
?>

<div class="full">
	<div class="submenu_container">
		<a class="submenu_button" href='/admin/moder/comments/view'>Комментарии</a>
		<a class="submenu_button submenu_button_active" href='/admin/moder/news/view'>Новости</a>
	</div>
	<div class="group_container">
		<a href="?show=all" class="group all_news <?php if ($show == "all") echo "active";?>">Все</a>
		<a href="?show=new" class="group new <?php if ($show != "all" && $show != "moder") echo "active";?>">Новые</a>
		<a href="?show=moder" class="group moder_news <?php if ($show == "moder") echo "active";?>">Утвержденные</a>
	</div>
	<?php
		if ($creator_id){
			$creator = $db->getCell("select `login` from `users` where `user_id` = '$creator_id' limit 1");
			echo "<div class='group_container'><div class='filter_button'>$creator <a href='?".$route->getGet("user")."'></a></div></div>";
		}
	?>
	<div id="moderationrecords">
		<?php
		for ($i = 0; $i < count($news); $i++)
		{
			?><div id="comment_<?php echo $news[$i]['news_id']; ?>" class="moderationrecord">
				<div class="recorddate left"><?php echo date("d.m.Y",$news[$i]['time'])."<br /><span style='font-size:9pt;color:black'>Приоритет: ".$news[$i]['order']."</span>"; ?></div>
				<div class="record left">
					<div class="recordtitle"><?php echo "<a target='_blank' href='/news/".$news[$i]['news_id']."-".translitIt($news[$i]['title'])."'>".$news[$i]['title']."</a>"; ?></div>
					<div class="recorduser">добавил: <?php echo "<a href='?".$route->getGet("user")."user=".$news[$i]['user_id']."'>".$news[$i]['login']."</a>"; ?></div>
					<div class="recordtext"><?php echo $news[$i]['preview']; ?></div>
				</div>
				<div class="recordbuttons right"><?php
					if ($news[$i]['moder'] == 0)
						echo "<a class='moder_buttons rapprove' title='Утвердить' href='/admin/moder/news/approve/".$news[$i]['news_id']."'></a>";
					elseif ($news[$i]['moder'] == 1)
						echo "<a class='moder_buttons runapprove' title='Не отмодерировано' href='/admin/moder/news/unapprove/".$news[$i]['news_id']."'></a>";
					?>
					<a class="moder_buttons redit" title='Редактировать' href='/admin/content/edit/<?php echo $news[$i]['news_id']; ?>'></a>
					<a class="moder_buttons rdelete" title='Удалить' href='/admin/moder/news/delete/<?php echo $news[$i]['news_id']; ?>'></a>
				</div>
			</div><?php
		}
		echo showPages($count,$limit);
	?></div>
</div>

<?php require_once ADMIN.DS."footer.php"; ?>